<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.yc.comm.Constant"%>
<%
request.setCharacterEncoding("utf-8");
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String strUserRoleName = (String)session.getAttribute(Constant.USER_ROLE);
String strUserName = (String)session.getAttribute(Constant.USER_NAME);
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">    
    <link rel="stylesheet" type="text/css" href="<%=path%>/js/ext/resources/css/ext-all.css">  
	<link rel="stylesheet" type="text/css" href="<%=path%>/js/ext/resources/css/edit-grid.css">  
	<script type="text/javascript" src="<%=path%>/js/ext/adapter/ext/ext-base.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/ext-all.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/DWRTreeLoader.js"></script>
	<script type="text/javascript" src="<%=path%>/js/ext/query-dwr-proxy.js"></script>	
	<script type='text/javascript' src='/btsm/dwr/interface/JFiberWeb.js'></script>
	<script type='text/javascript' src='/btsm/dwr/engine.js'></script>
	<script type='text/javascript' src='/btsm/dwr/util.js'></script>	 		
  </head>
<body>  
<%
	String strId = request.getParameter("id");
	String strName = request.getParameter("name");
%> 

<form id="showSectionForm" action="<%=path%>/pages/fiber/SectionOutInfo.jsp">
	<input type="hidden" name="id" id="strSectionID">
	<input type="hidden" name="name" id="strSectionName">
</form>
 
<div id="contentsDiv" style="height:100%"/>

<div id="sectionListWin">
    <div id="otherSectionList"/>
</div>

<script type="text/javascript">

var	ds;
var otherDS;
var simpleForm ;
var winList;
Ext.form.Field.prototype.msgTarget = 'side'; 
Ext.onReady(function()
{
    
	 var viewport = new Ext.Viewport({
	    layout:'border',
	    frame:true,
	    items:[ 	    	
	    	{
		        region: 'center',
		        contentEl: 'contentsDiv',
		        margins:'110 0 30 5',
		        split: true,
		        border: false	
		    }]
		});
		
	var sm = new Ext.grid.CheckboxSelectionModel({singleSelect: true});	
	var recordType = Ext.data.Record.create([								
                			   {name: 'id', type: "string"},
							   {name: 'linkID', type: "string"},
							   {name: 'ocID', type: "string"},
                	           {name: 'ocName', type: "string"},
                	           {name: 'ocBeginName', type: "string"},
							   {name: 'ocBeginLng', type: "string"},
							   {name: 'ocBeginLat', type: "string"},
                	           {name: 'ocEndName', type: "string"},
							   {name: 'ocEndLng', type: "string"},
							   {name: 'ocEndLat', type: "string"},
							   {name: 'ocType', type: "string"},
							   {name: 'ocNum', type: "string"},
                	           {name: 'ocLength', type: "string"},
                	           {name: 'ocInspectorName', type: "string"},
							   {name: 'notes', type: "string"},
                	           {name: 'ocInspectorPhone', type: "string"}
                	     ]);

	var cm  = new Ext.grid.ColumnModel(
			[
				sm,	            
	            {header: "中继段名称",  sortable: true, dataIndex: 'ocName',align:'left'},
	            {header: "起点名称",  sortable: true, dataIndex: 'ocBeginName',align:'left'},
	            {header: "终点名称",  sortable: true, dataIndex: 'ocEndName',align:'left'},
	            {header: "中继段长度",  sortable: true, dataIndex: 'ocLength',align:'left'},
				{header: "路由信息",  sortable: true, dataIndex: 'notes',align:'left'},
	            {header: "巡检员姓名",  sortable: true, dataIndex: 'ocInspectorPhone',align:'left'} 		
			]
		);

	
	function showDelete(id)
	{
		return "<input type='button' value='删除' class='x-btn button' onclick='doDeleteSectionFromLink(\"" + id  +   "\")'>";  
	}

	ds = new Ext.data.Store({
        proxy: new Ext.data.DWRProxy(JFiberWeb.getSectionInfoList, true),	
        reader: new Ext.data.ListRangeReader({
            totalProperty: 'totalSize',
            id: 'id'
        }, recordType),
        sortInfo:{field:'id', direction:'ASC'}// 排序信息
    });


	var contentDiv = Ext.get("contentsDiv");
	var sectionGrid = new Ext.grid.GridPanel({
	    	el: 'contentsDiv',	    	
	        ds: ds,
	        cm: cm,
			sm: sm,
			title: '<%=strName%>', 
			width: contentDiv.getComputedWidth(),
			height: contentDiv.getComputedHeight(), 
	        frame:true,
	        collapsible:false,
			autoShow :true, 
			autoExpandColumn:1, 
	        enableDragDrop:false,  
			tbar:[
				{
					text:'后退',
					pressed:false,					 
					handler:function()
					{
						history.go(-1);
					}
				} 
				],
			bbar: new Ext.PagingToolbar({
			        pageSize: 17,
			        store: ds,
			        displayInfo: true,
			        displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
			        emptyMsg: "没有记录"
			    })
					
					
				
	    });
    
	sectionGrid.render(); 
	ds.on('beforeload', function() {
		  ds.baseParams = {
				'<%=Constant.USER_ROLE%>':'<%=strUserRoleName%>',
				'<%=Constant.USER_ID%>':'<%=strUserName%>',
				id:'<%=strId%>'
		  };
		});		
	
	ds.load({params:{id:'<%=strId%>',start:0,limit:17}});	
    cm.setHidden(5,true);
	
	
	var otherRecordType = Ext.data.Record.create([								
                			   {name: 'id', type: "string"},
							   {name: 'ocID', type: "string"},							   
                	           {name: 'ocName', type: "string"},
                	           {name: 'ocBeginName', type: "string"},
							   {name: 'ocBeginLng', type: "string"},
							   {name: 'ocBeginLat', type: "string"},
                	           {name: 'ocEndName', type: "string"},
							   {name: 'ocEndLng', type: "string"},
							   {name: 'ocEndLat', type: "string"},
							   {name: 'canEdit', type: "string"}
                	     ]);

	var otherCM  = new Ext.grid.ColumnModel(
			[
				sm,	            
	            {header: "中继段名称", sortable: true, dataIndex: 'ocName',align:'left'},
	            {header: "起点名称",  sortable: true, dataIndex: 'ocBeginName',align:'left'},
				{header: "起点经度",  sortable: true, dataIndex: 'ocBeginLng',align:'left'},
				{header: "起点纬度",  sortable: true, dataIndex: 'ocBeginLat',align:'left'},
	            {header: "终点名称", sortable: true, dataIndex: 'ocEndName',align:'left'}, 
				{header: "终点经度",  sortable: true, dataIndex: 'ocEndLng',align:'left'}, 	
				{header: "终点纬度", sortable: true, dataIndex: 'ocEndLat',align:'left'} 	
			]
		);
 
	otherDS = new Ext.data.Store({
        proxy: new Ext.data.DWRProxy(JFiberWeb.getOtherSectionInfoList, true),	
        reader: new Ext.data.ListRangeReader({
            totalProperty: 'totalSize',
            id: 'id'
        }, otherRecordType),
        sortInfo:{field:'id', direction:'ASC'}// 排序信息
    });

	function showDel(value, cellmeta, record, rowIndex, columnIndex, store)
	{
		var id = record.get("id");
		var canEdit = record.get("canEdit");
		if(canEdit === '<%=Constant.EDIT_YES%>' )
			return "<input type='button' value='删除' class='x-btn button' onclick='doDeleteSection(\"" + id  +   "\")'>";  
		else
			return "---";
	}	
		
	     
	function togglePreview(pressed){
        var preview = Ext.getCmp('preview');
        preview[pressed ? 'show' : 'hide']();
        preview.ownerCt.doLayout();
    }
	
	
	
	var selectOcID;
	function addSectionForLink()
	{
		if(!winList){
            winList = new Ext.Window({
                applyTo:'sectionListWin',
                layout:'fit',
                width:600,
                height:400,
                closeAction:'hide',
				modal : true,
                plain: true,
				buttonAlign :'left',
				items:new Ext.grid.GridPanel({ 	    	
							ds: otherDS,
							cm: otherCM,
							sm: new Ext.grid.RowSelectionModel({
								//singleSelect: true,
								listeners: {
									rowselect: function(sm, row, rec) { 
										//selectOcID = rec.get("ocID");
										selectOcID = sm.getSelections();
									}
								}
							}),
							title: '其他中继段信息', 
							height:600,
							frame:true,
							closable :false,  
							columnLines :true,
							width:800,
							enableColumnResize :true,
							enableDragDrop:false,   
							bbar: new Ext.PagingToolbar({
									pageSize: 17,
									store: otherDS,
									displayInfo: true,
									displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
									emptyMsg: "没有记录"
								})
									
									
								
						}),
                buttons: [{
                    text:'添加',
                    handler: function()
                    {
						var selectIDArray = new Array();
						for(var i=0; i<selectOcID.length; i++)
							selectIDArray.push(selectOcID[i].get("ocID") );
						
	                	JFiberWeb.updateSectionInfoForLinkID(selectIDArray,'<%=strId%>', function(data) {
		           			 if(data) {							    	     		
		           				 ds.load({params:{start:0,limit:17}});
		           				 winList.hide();
		           			 }
	           			 });
                        
                    }
                },{
                    text: '取消',
                    handler: function(){
                        winList.hide();
                    }
                }]
            });
        }
        winList.show(this);
		otherDS.on('beforeload', function() {
		  otherDS.baseParams = {
				'<%=Constant.USER_ROLE%>':'<%=strUserRoleName%>',
				'<%=Constant.USER_ID%>':'<%=strUserName%>',
				id:'<%=strId%>'
		  };
		});	
		otherDS.load({params:{id:'<%=strId%>',start:0,limit:17}});
	}
		
	 
	
})


function doDeleteSectionFromLink(id)
{
	Ext.MessageBox.confirm('确认删除', '确认从该光纤（环）删除该中继段）?', 
		function(btn) {
		 if(btn == 'yes') // 选中了是按钮	
		 {	    	     	
			 JFiberWeb.deleteSectionInfoForLinkID('<%=strId%>',id, function(data) {
			 if(data) {							    	     		
				ds.load({params:{id:'<%=strId%>',start:0,limit:17}});
			 }else 
			 {
				Ext.Msg.alert("删除失败","数据删除失败!");
			 }
			 });
			
		 }
		}
	);
}

function doDeleteSection(id)
{
	JFiberWeb.isSectionHaveFiber(id,function(data){
		if(data)
		{
			Ext.MessageBox.show({
			    title:"提示",
			    msg:"该中继段具有户外信息，请删除户外信息后再删除该中继段!", 
			    icon:Ext.MessageBox.WARNING
			});			
			return;
		}else
		{
			Ext.MessageBox.confirm('确认删除', '确认删除该中继段?', 
				function(btn) {
				 if(btn == 'yes') // 选中了是按钮	
				 {	    	     	
					 JFiberWeb.deleteSectionInfo(id, function(data) {
					 if(data) {							    	     		
						 ds.load({params:{start:0,limit:17}});
						 otherDS.load({params:{start:0,limit:17}});
					 }else 
					 {
						Ext.Msg.alert("删除失败","数据删除失败!");
					 }
					 });
					
				 }
				}
			);
		}	
	})
	
}

function doShowSection(id,name)
{
	document.getElementById("strSectionID").value = id; 
	document.getElementById("strSectionName").value = name; 
	document.getElementById("showSectionForm").action = "<%=path%>/pages/fiber/SectionOutInfo.jsp";
	document.getElementById("showSectionForm").submit();
}

</script>

</body>
</html>